<script setup>
import {ref,watch,onMounted} from 'vue';
import { queryPageApi,addempApi,queryInfoApi,updateApi,deleteApi } from '@/api/emp'; 
import { queryAllDept as queryAllDeptApi } from '@/api/dept.js';
import { ElMessage,ElMessageBox } from 'element-plus';

const searchEmp=ref({name:'',gender:'',date:[],begin:'',end:''});
const empList=ref([]);
//部门列表数据
const depts=ref([]);

//token
const token=ref('');

const search= async ()=>{
  const result = await queryPageApi(searchEmp.value.name,searchEmp.value.gender,searchEmp.value.begin,
                     searchEmp.value.end,currentPage.value,pageSize.value);
  if(result.code){
    empList.value=result.data.rows;
    total.value=result.data.total;
  }
};
const searchDepts= async ()=>{
  const result = await queryAllDeptApi();
  if(result.code){
    depts.value=result.data;
  }
};

onMounted(()=>{
  search();
  searchDepts();
  getToken();
})


const getToken=()=>{
  //从本地存储中获取token
  let loginUser=JSON.parse(localStorage.getItem('loginUser'));
  if(loginUser && loginUser.token){
    token.value=loginUser.token;
  }
};

const clear=()=>{
  searchEmp.value={name:'',gender:'',date:[],begin:'',end:''};
  search();
};

//侦听searchEmp的date属性
watch(() => searchEmp.value.date,(newVal,oldVal)=>{
  if( newVal.length!==null && newVal.length==2){
    searchEmp.value.begin=newVal[0];
    searchEmp.value.end=newVal[1];
  }else{
    searchEmp.value.begin='';
    searchEmp.value.end='';
  }
});


const currentPage = ref(1) // 当前页
const pageSize = ref(10) // 每页显示条目数
const background = ref(true)  /// 是否显示背景
const total=ref(0); // 总条数

const handleSizeChange = (val) => {
  search();
}
const handleCurrentChange = (val) => {
  search();
}


//新增/修改表单
const employee = ref({
  username: '',
  name: '',
  gender: '',
  phone: '',
  job: '',
  salary: '',
  deptId: '',
  entryDate: '',
  image: '',
  exprList: []
})
//职位
const jobs=ref([{name:'班主任',value:1},{name:'讲师',value:2},{name:'学工主管',value:3},{name:'教研主管',value:4},
{name:'咨询师',value:5},{name:'其他',value:6}]);

// 控制弹窗
const dialogVisible = ref(false)
const dialogTitle = ref('新增员工')

//新增员工
const addEmp=()=>{
  dialogVisible.value=true;
  dialogTitle.value='新增员工';
  employee.value={
    username: '',
    name: '',
    gender: '',
    phone: '',
    job: '',
    salary: '',
    deptId: '',
    entryDate: '',
    image: '',
    exprList: []
  }
  
  if(empFromref.value){
    empFromref.value.resetFields();
  }

};

//文件上传
// 图片上传成功后触发
const handleAvatarSuccess = (response) => {  //response服务器返回来的响应体
  employee.value.image=response.data;
}
// 文件上传之前触发
const beforeAvatarUpload = (rawFile) => { //rawFile上传的文件,函数返回false上传失败
  if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png') {
    ElMessage.error('只支持上传图片')
    return false
  } else if (rawFile.size / 1024 / 1024 > 10) {
    ElMessage.error('只能上传10M以内图片')
    return false
  }
  return true
}

//添加工作经历
const addExprItem=()=>{
  employee.value.exprList.push({company:'',job:'',begin:'',end:'',exprDate:[]});
}
//删除工作经历
const delExprItem=(index)=>{
  employee.value.exprList.splice(index,1);
}
//侦听事件
watch(() => employee.value.exprList,(newVal,oldVal)=>{
  if(employee.value.exprList && employee.value.exprList.length>0){
    employee.value.exprList.forEach((e)=>{
      e.begin=e.exprDate[0];
      e.end=e.exprDate[1];
    });
  }
},{deep:true}); //深度侦听

const save= async ()=>{
  if(!empFromref.value) return ;
  empFromref.value.validate( async (valid)=>{
      if(valid){

          let result;
          if(employee.value.id){
            result= await updateApi(employee.value);
          }else{
            result= await addempApi(employee.value);
          }

          if(result.code){
            ElMessage.success("保存成功");
            dialogVisible.value=false;
            search(); //刷新
          }else{
            ElMessage.error(msg);
          }
      }else{
          ElMessage.error("请按正确的格式!");
      }
  });
};

//表单校验规则
const rules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '用户名长度应在2到20个字符之间', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '姓名长度应在2到10个字符之间', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号', trigger: 'blur' }
  ]
});

const empFromref=ref();

const edit= async (id)=>{
  if(empFromref.value){
    empFromref.value.resetFields();
  }
  const result= await queryInfoApi(id);
  if(result.code){
    dialogVisible.value=true;
    dialogTitle.value='编辑员工';
    employee.value=result.data;

    //对工作经历进行处理
    let exprList=employee.value.exprList;
    if(exprList && exprList.length>0){
      exprList.forEach((expr)=>{
          expr.exprDate=[expr.begin,expr.end];
      });
    }
  }
};

const deleteById=(id)=>{
  ElMessageBox.confirm(
    '你真的要删除吗?',
    'Warning',
    {
      confirmButtonText: 'OK',
      cancelButtonText: 'Cancel',
      type: 'warning',
    }
  ).then( //确认删除
    async () => {
      const result= await deleteApi(id);
      if(result.code){
        ElMessage.success("删除成功");
        search();
      }else{
        ElMessage.error("删除失败, "+result.msg);
      }
    }
  ).catch( //取消删除
    ()=>{
      ElMessage.info('删除取消');
    }
  );

};

const ids=ref([]);
//表格选择事件
const handleSelectionChange=(selection)=>{ //: 选中的行数据
  ids.value=selection.map(item=>item.id); 
};

const deleteByIds=()=>{
  ElMessageBox.confirm(
    '你真的要删除吗?',
    'Warning',
    {
      confirmButtonText: 'OK',
      cancelButtonText: 'Cancel',
      type: 'warning',
    }
  ).then(
    async () => {
        if(ids.value && ids.value.length>0){
          const result= await deleteApi(ids.value);
          if(result.code){
            ElMessage.success("删除成功");
            search();
          }else{
            ElMessage.error("删除失败, "+result.msg);
          }
        }else{
            ElMessage.error("请选择要删除的记录");
        }
    }
  ).catch(
      () => {
        ElMessage.info('删除取消');
      }
  );
};

</script>

<template>
  <h1>员工管理</h1>
  <!-- 搜索栏 -->
   <div class="container">
    <el-form :inline="true" :model="searchEmp" class="demo-form-inline">
    <el-form-item label="姓名:">
      <el-input v-model="searchEmp.name" placeholder="请输入员工姓名" clearable style="width: 150px;"/>
    </el-form-item>
    <el-form-item label="性别:">
      <el-select v-model="searchEmp.gender" placeholder="选择性别" clearable style="width: 100px;">
        <el-option label="男" value="1" />
        <el-option label="女" value="2" />
      </el-select>
    </el-form-item>
    <el-form-item label="入职日期">
      <el-date-picker
        v-model="searchEmp.date"
        type="daterange"
        range-separator="到"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        style="width: 230px;"
        value-format="YYYY-MM-DD"
        clearable
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="search">查询</el-button>
      <el-button type="warning" @click="clear">清空</el-button>
    </el-form-item>
  </el-form>
   </div>
   <div class="container">
      <el-button type="primary" @click="addEmp">+ 新增员工</el-button>
      <el-button type="danger" @click="deleteByIds">- 批量删除员工</el-button>
   </div>
   <div class="container">
      <el-table :data="empList" border style="width: 100%" @selection-change="handleSelectionChange"> 
        <!-- 多选框 -->
        <el-table-column type="selection" width="55" /> 
        <el-table-column prop="name" label="姓名" width="120" align="center"/>
        <el-table-column prop="gender" label="性别" width="120" align="center">
          <template #default="scope">{{scope.row.gender==1 ? '男' : '女'}}</template>
        </el-table-column>
        <el-table-column prop="image" label="头像"  width="120" align="center">
          <template #default="scope">
            <el-avatar :size="40">
              <img
                :src="scope.row.image"
              />
            </el-avatar>
          </template>
        </el-table-column>
        <el-table-column prop="deptName" label="所属部门"  width="120" align="center"/>
        <el-table-column prop="job" label="职位"  width="120" align="center">
            <template #default="scope">
                <span v-if="scope.row.job==1">班主任</span>
                <span v-else-if="scope.row.job==2">讲师</span>
                <span v-else-if="scope.row.job==3">学工主管</span>
                <span v-else-if="scope.row.job==4">教研主管</span>
                <span v-else-if="scope.row.job==5">咨询师</span>
                <span v-else="scope.row.job=6">其他</span>
            </template>
        </el-table-column>
        <el-table-column prop="entryDate" label="入职日期"  width="120" align="center"/>
        <el-table-column prop="updateTime" label="最新操作时间"  width="180" align="center"/>
        <el-table-column label="操作"  width="180" align="center">
          <template #default="scope">
          <el-button type="primary" size="small" @click="edit(scope.row.id)"><el-icon><EditPen /></el-icon>编辑</el-button>
          <el-button type="danger" size="small" @click="deleteById(scope.row.id)"><el-icon><Delete /></el-icon>删除</el-button>
        </template>
        </el-table-column>
      </el-table>
   </div>
   <!-- 分页条 -->
   <div class="container">
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5,10,15,20,30,40,50]"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
   </div>
   <!-- 新增员工/修改员工对话框 -->
   <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="employee" :rules="rules" ref="empFromref" label-width="80px">
        <!-- 基本信息 -->
        <!-- 第一行 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="employee.username" placeholder="请输入员工用户名"></el-input>
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="employee.name" placeholder="请输入员工姓名"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第二行 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="employee.gender" placeholder="请选择性别" style="width: 100%;">
                <el-option label="男" value="1"></el-option>
                <el-option label="女" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="employee.phone" placeholder="请输入员工手机号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第三行 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="职位">
              <el-select v-model="employee.job" placeholder="请选择职位" style="width: 100%;">
                <el-option v-for=" job in jobs" :key="job.value" :label="job.name" :value="job.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="薪资">
              <el-input v-model="employee.salary" placeholder="请输入员工薪资"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第四行 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所属部门">
              <el-select v-model="employee.deptId" placeholder="请选择部门" style="width: 100%;">
                <el-option v-for=" d in depts" :key="d.id" :label="d.name" :value="d.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入职日期">
              <el-date-picker v-model="employee.entryDate" type="date" style="width: 100%;" placeholder="选择日期" format="YYYY-MM-DD" value-format="YYYY-MM-DD"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第五行 -->
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="头像">
              <el-upload
                class="avatar-uploader"
                action="/api/upload" 
                :headers="{'token':token}"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                >
                <img v-if="employee.image" :src="employee.image" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
        

        <!-- 工作经历 -->
        <!-- 第六行 -->
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="工作经历">
              <el-button type="success" size="small" @click="addExprItem">+ 添加工作经历</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        
        <!-- 第七行 ...  工作经历 -->
        <el-row :gutter="3" v-for=" (e,index) in employee.exprList">
          <el-col :span="10">
            <el-form-item size="small" label="时间" label-width="80px">
              <el-date-picker type="daterange" v-model="e.exprDate"  range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" format="YYYY-MM-DD" value-format="YYYY-MM-DD" ></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item size="small" label="公司" label-width="60px">
              <el-input v-model="e.company" placeholder="请输入公司名称"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item size="small" label="职位" label-width="60px">
              <el-input v-model="e.job" placeholder="请输入职位"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="2">
            <el-form-item size="small" label-width="0px">
              <el-button type="danger" @click="delExprItem(index)" >- 删除</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      
      <!-- 底部按钮 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">保存</el-button>
        </span>
      </template>
  </el-dialog> 

</template>

<style scoped>
  .container{
    margin-top: 15px;
  }
  
  .avatar {
    height: 40px;
  }
  .avatar-uploader .avatar {
    width: 78px;
    height: 78px;
    display: block;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }

  .avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
  }

  .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 78px;
    height: 78px;
    text-align: center;
    border-radius: 10px;
    /* 添加灰色的虚线边框 */
    border: 1px dashed var(--el-border-color);
  }
    


</style>